/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY. 
 * See the GNU General Public License for more details.
 * 
 * Copyright (C) 2011 CellObject.net
 */
package net.cellobject.javacodeformatter.model;

import net.cellobject.javacodeformatter.log.FormatterLog;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.jdt.core.IClassFile;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;

public abstract class FormatterItemType implements Comparable<FormatterItemType> {
	private static final ISharedImages PLATFORM_IMAGES = PlatformUI.getWorkbench().getSharedImages();

	private final String id;

	private final String printName;

	private final int ordinal;

	private FormatterItemType(final String id, final String name, final int position) {
		this.id = id;
		this.ordinal = position;
		this.printName = name;
	}

	public String getId() {
		return id;
	}

	public String getName() {
		return printName;
	}

	public abstract Image getImage();

	public abstract IFormatterItem newFavorite(Object obj);

	public abstract IFormatterItem loadFavorite(String info);

	public int compareTo(final FormatterItemType other) {
		return this.ordinal - other.ordinal;
	}

	////////////////////////////////////////////////////////////////////////////
	//
	// Constants representing types of favorites objects.
	//
	////////////////////////////////////////////////////////////////////////////
	public static final FormatterItemType UNKNOWN = new FormatterItemType("Unknown", "Unknown", 0) {
		@Override
		public Image getImage() {
			return null;
		}

		@Override
		public IFormatterItem newFavorite(final Object obj) {
			return null;
		}

		@Override
		public IFormatterItem loadFavorite(final String info) {
			return null;
		}
	};

	public static final FormatterItemType WORKBENCH_FILE = new FormatterItemType("WBFile", "Workbench File", 1) {
		@Override
		public Image getImage() {
			return PLATFORM_IMAGES.getImage(org.eclipse.ui.ISharedImages.IMG_OBJ_FILE);
		}

		@Override
		public IFormatterItem newFavorite(final Object obj) {
			if (! (obj instanceof IFile)) {
				return null;
			}
			return new FormatterResource(this, (IFile)obj);
		}

		@Override
		public IFormatterItem loadFavorite(final String info) {
			return FormatterResource.loadFavorite(this, info);
		}
	};

	public static final FormatterItemType WORKBENCH_FOLDER = new FormatterItemType("WBFolder", "Workbench Folder", 2) {
		@Override
		public Image getImage() {
			return PLATFORM_IMAGES.getImage(org.eclipse.ui.ISharedImages.IMG_OBJ_FOLDER);
		}

		@Override
		public IFormatterItem newFavorite(final Object obj) {
			if (! (obj instanceof IFolder)) {
				return null;
			}
			return new FormatterResource(this, (IFolder)obj);
		}

		@Override
		public IFormatterItem loadFavorite(final String info) {
			return FormatterResource.loadFavorite(this, info);
		}
	};

	public static final FormatterItemType WORKBENCH_PROJECT = new FormatterItemType("WBProj", "WorkbenchProject", 3) {
		@Override
		public Image getImage() {
			return PLATFORM_IMAGES.getImage(IDE.SharedImages.IMG_OBJ_PROJECT);
		}

		@Override
		public IFormatterItem newFavorite(final Object obj) {
			if (! (obj instanceof IProject)) {
				return null;
			}
			return new FormatterResource(this, (IProject)obj);
		}

		@Override
		public IFormatterItem loadFavorite(final String info) {
			return FormatterResource.loadFavorite(this, info);
		}
	};

	public static final FormatterItemType JAVA_PROJECT = new FormatterItemType("JProj", "Java Project", 4) {
		@Override
		public Image getImage() {
			return PLATFORM_IMAGES.getImage(IDE.SharedImages.IMG_OBJ_PROJECT);
		}

		@Override
		public IFormatterItem newFavorite(final Object obj) {
			if (! (obj instanceof IJavaProject)) {
				return null;
			}
			return new FormatterJavaElement(this, (IJavaProject)obj);
		}

		@Override
		public IFormatterItem loadFavorite(final String info) {
			return FormatterJavaElement.loadFavorite(this, info);
		}
	};

	public static final FormatterItemType JAVA_PACKAGE_ROOT = new FormatterItemType("JPkgRoot", "Java Package Root", 5) {
		@Override
		public Image getImage() {
			return PLATFORM_IMAGES.getImage(org.eclipse.ui.ISharedImages.IMG_OBJ_FOLDER);
		}

		@Override
		public IFormatterItem newFavorite(final Object obj) {
			if (! (obj instanceof IPackageFragmentRoot)) {
				return null;
			}
			return new FormatterJavaElement(this, (IPackageFragmentRoot)obj);
		}

		@Override
		public IFormatterItem loadFavorite(final String info) {
			return FormatterJavaElement.loadFavorite(this, info);
		}
	};

	public static final FormatterItemType JAVA_PACKAGE = new FormatterItemType("JPkg", "Java Package", 6) {
		@Override
		public Image getImage() {
			return org.eclipse.jdt.ui.JavaUI.getSharedImages().getImage(org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_PACKAGE);
		}

		@Override
		public IFormatterItem newFavorite(final Object obj) {
			if (! (obj instanceof IPackageFragment)) {
				return null;
			}
			return new FormatterJavaElement(this, (IPackageFragment)obj);
		}

		@Override
		public IFormatterItem loadFavorite(final String info) {
			return FormatterJavaElement.loadFavorite(this, info);
		}
	};

	public static final FormatterItemType JAVA_CLASS_FILE = new FormatterItemType("JClass", "Java Class File", 7) {
		@Override
		public Image getImage() {
			return org.eclipse.jdt.ui.JavaUI.getSharedImages().getImage(org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_CFILE);
		}

		@Override
		public IFormatterItem newFavorite(final Object obj) {
			if (! (obj instanceof IClassFile)) {
				return null;
			}
			return new FormatterJavaElement(this, (IClassFile)obj);
		}

		@Override
		public IFormatterItem loadFavorite(final String info) {
			return FormatterJavaElement.loadFavorite(this, info);
		}
	};

	public static final FormatterItemType JAVA_COMP_UNIT = new FormatterItemType("JCompUnit", "Java Compilation Unit", 8) {
		@Override
		public Image getImage() {
			return org.eclipse.jdt.ui.JavaUI.getSharedImages().getImage(org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_CUNIT);
		}

		@Override
		public IFormatterItem newFavorite(final Object obj) {
			if (! (obj instanceof ICompilationUnit)) {
				return null;
			}
			return new FormatterJavaElement(this, (ICompilationUnit)obj);
		}

		@Override
		public IFormatterItem loadFavorite(final String info) {
			return FormatterJavaElement.loadFavorite(this, info);
		}
	};

	public static final FormatterItemType JAVA_INTERFACE = new FormatterItemType("JInterface", "Java Interface", 9) {
		@Override
		public Image getImage() {
			return org.eclipse.jdt.ui.JavaUI.getSharedImages().getImage(org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_INTERFACE);
		}

		@Override
		public IFormatterItem newFavorite(final Object obj) {
			if (! (obj instanceof IType)) {
				return null;
			}
			try {
				if (! ((IType)obj).isInterface()) {
					return null;
				}
			} catch (final JavaModelException e) {
				FormatterLog.logError(e);
			}
			return new FormatterJavaElement(this, (IType)obj);
		}

		@Override
		public IFormatterItem loadFavorite(final String info) {
			return FormatterJavaElement.loadFavorite(this, info);
		}
	};

	public static final FormatterItemType JAVA_CLASS = new FormatterItemType("JClass", "Java Class", 10) {
		@Override
		public Image getImage() {
			return org.eclipse.jdt.ui.JavaUI.getSharedImages().getImage(org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_CLASS);
		}

		@Override
		public IFormatterItem newFavorite(final Object obj) {
			if (! (obj instanceof IType)) {
				return null;
			}
			try {
				if ( ((IType)obj).isInterface()) {
					return null;
				}
			} catch (final JavaModelException e) {
				FormatterLog.logError(e);
			}
			return new FormatterJavaElement(this, (IType)obj);
		}

		@Override
		public IFormatterItem loadFavorite(final String info) {
			return FormatterJavaElement.loadFavorite(this, info);
		}
	};

	////////////////////////////////////////////////////////////////////////////
	//
	// Accessors for all Favorite types
	//
	////////////////////////////////////////////////////////////////////////////
	private static final FormatterItemType[] TYPES = {UNKNOWN, WORKBENCH_FILE, WORKBENCH_FOLDER, WORKBENCH_PROJECT, JAVA_PROJECT, JAVA_PACKAGE_ROOT, JAVA_PACKAGE, JAVA_CLASS_FILE, JAVA_COMP_UNIT, JAVA_INTERFACE, JAVA_CLASS,};

	public static FormatterItemType[] getTypes() {
		return TYPES;
	}
}
